package com.cadre.web.controller.officer;

import com.cadre.common.utils.R;
import com.cadre.common.utils.common.GenerateUUID;
import com.cadre.common.utils.oldutil.DateUtil;
import com.cadre.common.utils.oldutil.ValidatorUtils;
import com.cadre.system.domain.officer.A22;
import com.cadre.system.service.A22Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = {"政治面貌接口"})
@RequestMapping({"/profess/a22"})
@RestController
public class A22Controller {
    @Resource
    private A22Service a22Service ;

    @GetMapping({"/info/{flowid}"})
    public R info(@PathVariable("flowid") String flowid) {
        return R.ok().put("a22", (Object) ((A22) this.a22Service.getBaseMapper().selectById(flowid)));
    }

    @GetMapping({"/infoByPersonId/{personId}"})
    public R infoByPersonId(@PathVariable("personId") String personId) {
        return R.ok().put("data", (Object) this.a22Service.selectListByPersonId(personId));
    }

    @GetMapping({"/infos/{personid}"})
    @ApiOperation("根据人员ID获取信息")
    public R infos(@PathVariable("personid") String personid) {
        Map<String, Object> map = new HashMap<>();
        map.put("personid", personid);
        map.put("serial", 1);
        List<A22> list1 = this.a22Service.selectByMap(map);
        map.put("serial", 2);
        List<A22> list2 = this.a22Service.selectByMap(map);
        map.put("serial", 3);
        List<A22> list3 = this.a22Service.selectByMap(map);
        Map<String, Object> map2 = new HashMap<>();
        if (list1.size() != 0) {
            map2.put("dydp", list1.get(0).getA2205());
            map2.put("sj", DateUtil.dateToString(list1.get(0).getA2210(), DateUtil.MONTG_DATE_FORMAT));
        }
        if (list2.size() != 0) {
            map2.put("drdp", list2.get(0).getA2205());
        }
        if (list3.size() != 0) {
            map2.put("dsdp", list3.get(0).getA2205());
        }
        return R.ok().put("data", (Object) map2);
    }

    @PostMapping({"/save"})
    public R save(@RequestBody A22 a22) {
        a22.setFlowid(GenerateUUID.getUUID());
        this.a22Service.getBaseMapper().insert(a22);
        return R.ok();
    }

    @PostMapping({"/saveList"})
    @ApiOperation("政治面貌多条保存")
    public R saveList(@RequestBody Map<String, Object> map) {
        String[] keys = {"dydp", "sj", "drdp", "dsdp", "personid"};
        String[] values = new String[keys.length];

        for (int i = 0; i < keys.length; i++) {
            Object value = map.get(keys[i]);
            if (value != null && value != "") {
                values[i] = value.toString();
            }else{
                values[i] = null;
            }

        }

        String dydp = values[0];
        String sj = values[1];
        String drdp = values[2];
        String dsdp = values[3];
        String personid = values[4];
        if (!(dydp == null || dydp == "")) {
            A22 a22 = new A22();
            a22.setFlowid(GenerateUUID.getUUID());
            a22.setA2205(dydp);
            a22.setA2210(DateUtil.stringtoDate(sj, DateUtil.MONTG_DATE_FORMAT));
            a22.setPersonid(personid);
            a22.setSerial((short) 1);
            this.a22Service.insertSelective(a22);
        }
        if (!(drdp == null || drdp == "")) {
            A22 a222 = new A22();
            a222.setFlowid(GenerateUUID.getUUID());
            a222.setA2205(drdp);
            a222.setPersonid(personid);
            a222.setSerial((short) 2);
            this.a22Service.insertSelective(a222);
        }
        if (!(dsdp == null || dsdp == "")) {
            A22 a223 = new A22();
            a223.setFlowid(GenerateUUID.getUUID());
            a223.setA2205(dsdp);
            a223.setPersonid(personid);
            a223.setSerial((short) 3);
            this.a22Service.insertSelective(a223);
        }
        return R.ok();
    }

    @PostMapping({"/updateList"})
    @ApiOperation("修改")
    public R updateList(@RequestBody Map<String, Object> map) {
        map.get("dydp").toString();
        map.get("sj").toString();
        map.get("drdp").toString();
        map.get("dsdp").toString();
        String personid = map.get("personid").toString();
        if (!(personid == null || personid == "")) {
            if (this.a22Service.selectListByPersonId(personid).size() > 0) {
                this.a22Service.deleteByPersonId(personid);
            }
            saveList(map);
        }
        return R.ok();
    }

    @PostMapping({"/update"})
    public R update(@RequestBody A22 a22) {
        ValidatorUtils.validateEntity(a22, new Class[0]);
        this.a22Service.getBaseMapper().updateById(a22);
        return R.ok();
    }

    @GetMapping({"/delete"})
    public R delete(@RequestBody String[] personids) {
        this.a22Service.getBaseMapper().deleteBatchIds(Arrays.asList(personids));
        return R.ok();
    }
}
